# -*- coding: utf-8 -*-
# @Time : 2021/9/10 9:09
# @Author : 阿怪
# @Email : scg@gmail.com
# @File : CRM-ST-TJFX-055.py
# @Project : crm自动化测试项目实战
'''验证财务总计的搜索功能—正确输入搜索信息'''

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.alert import Alert  # 把弹窗封装成类Alert
import time
driver = webdriver.Chrome()
cookie = {'name': 'PHPSESSID', 'value': '0onrpth7nsd6mbi1hfgi99o2b1'} # 获取到cookid
driver.get('http://192.168.1.140/')  # 访问服务器
driver.add_cookie(cookie)
driver.get('http://192.168.1.140/crm3/index.php?m=dynamic&a=index')
#进入登录成功页面

#先去添加一条应收款，应收款之前需要有1个客户，1个商机，1个合同。

#     去添加个客户
driver.find_element(By.CSS_SELECTOR,'body > div.navbar.navbar-inverse.navbar-fixed-top > div > div > div.nav-collapse.collapse > ul:nth-child(1) > li:nth-child(2) > a').click()
sleep(2)   #进入客户首页

add_client_locator = (By.CSS_SELECTOR, 'body > div.container > div.row > div:nth-child(1) > div > a')  # 添加客户按钮定位器
driver.find_element(*add_client_locator).click()
driver.find_element(By.ID, 'name').send_keys('张啊123124') # 这里这个客户名不能重复，每一次都要改变。。
driver.find_element(By.NAME, 'submit').click()

#     去添加个商机
driver.find_element(By.CSS_SELECTOR,'body > div.navbar.navbar-inverse.navbar-fixed-top > div > div > div.nav-collapse.collapse > ul:nth-child(1) > li:nth-child(3) > a').click()
sleep(2)  #进入商机首页
add_opportunity_locator = (By.CSS_SELECTOR,'body > div.container > div.row > div:nth-child(1) > div > a')  #添加商机定位器
driver.find_element(*add_opportunity_locator).click()  #点击‘添加商机’
driver.find_element(By.CSS_SELECTOR,'#form1 > table > tbody > tr:nth-child(2) > td:nth-child(4)').click()   #点击‘客户’输入框
sleep(3)
driver.find_element(By.CSS_SELECTOR,'#datas > tr:nth-child(1) > td:nth-child(1) > input[type=radio]:nth-child(1)').click()  #选中刚刚添加的客户
driver.find_element(By.CSS_SELECTOR,'body > div:nth-child(17) > div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix > div > button:nth-child(1) > span').click()
  #回到商机编辑界面
driver.find_element(By.ID,'name').send_keys('测试商机12')    #商机名也需要每次更换
driver.find_element(By.ID,'estimate_price').send_keys('1000')
driver.find_element(By.NAME,'submit').click()

#     去添加个合同
driver.find_element(By.CSS_SELECTOR,'body > div.navbar.navbar-inverse.navbar-fixed-top > div > div > div.nav-collapse.collapse > ul:nth-child(1) > li:nth-child(7) > a').click()
sleep(2)
driver.find_element(By.CSS_SELECTOR,'body > div.container > div.row > div:nth-child(1) > div > a').click()
driver.find_element(By.XPATH,'//*[@id="business_name"]').click()
sleep(2)
driver.find_element(By.CSS_SELECTOR,'#data > tr:nth-child(1) > td:nth-child(1) > input[type=radio]:nth-child(1)').click()   #选中最新商机
driver.find_element(By.XPATH,'/html/body/div[7]/div[3]/div/button[1]/span').click()
driver.find_element(By.NAME,'submit').click()

#     去财务统计首页，添加一个应收款
driver.find_element(By.XPATH,'/html/body/div[1]/div/div/div[2]/ul[1]/li[8]/a').click()  #点击财务进入财务首页
sleep(2)
driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[1]/div[3]/a[1]').click()
sleep(2)
driver.find_element(By.ID,'name').send_keys('测试应收款')
driver.find_element(By.ID,'contract_name').click()  #点击合同输入框
sleep(2)
driver.find_element(By.XPATH,'//*[@id="data"]/tr[1]/td[1]/input').click()   #选择合同
sleep(3)
driver.find_element(By.XPATH,'/html/body/div[8]/div[3]/div/button[1]/span').click() #点击OK
sleep(2)
driver.find_element(By.ID,'price').clear()
sleep(3)
driver.find_element(By.XPATH,'//*[@id="price"]').send_keys('5000')
sleep(2)
driver.find_element(By.NAME,'submit').click()   #保存收款单

#    去财务统计界面搜索
driver.find_element(By.XPATH,'/html/body/div[5]/div[1]/ul/li[5]/a').click() #点击【财务】
department_locator = driver.find_element(By.ID,'department')  #部门定位器
role_locator = driver.find_element(By.ID,'role')    #选择员工定位器
start_time_locator = (By.ID,'start_time')    #开始时间定位器
end_time_locator = (By.ID,'end_time')     #结束时间定位器
submit_locator = (By.XPATH,'//*[@id="searchForm"]/ul/li[4]/button')   #【搜索】按钮定位器
today= time.strftime('%Y-%m-%d')   #获取当前日期

select = Select(department_locator)
select.select_by_visible_text('办公室')
sleep(1)
select2 = Select(role_locator)
select2.select_by_visible_text('CEO-admin')
sleep(2)

driver.find_element(*start_time_locator).send_keys(today)
driver.find_element(*end_time_locator).send_keys(today)
driver.find_element(*submit_locator).click()   #点击【搜索】
sleep(2)
 # 获取’应收金额‘，是否为 5000
payment_text =  driver.find_element(By.XPATH,'//*[@id="report_content"]/table/tbody/tr/td[5]/a').text
print(payment_text)
sleep(2)


#  回去财务首页把应收款数据清空
driver.find_element(By.XPATH,'/html/body/div[5]/div[1]/ul/li[1]/a').click()
check_all_locator = (By.ID,'check_all')   #全选按钮定位器
delete_locator = (By.ID,'delete')    #删除定位器
driver.find_element(*check_all_locator).click()   #点击全选
driver.find_element(*delete_locator).click()   #点击删除
alert = Alert(driver)
alert.accept()
sleep(2)

driver.quit()



